//
//  LXWaterFlowLayout.m
//  BonweBuy
//
//  Created by liuxin on 2020/2/25.
//  Copyright © 2020 liuxin. All rights reserved.
//

#import "LXWaterFlowLayout.h"

@implementation LXWaterFlowLayout

// 显示范围发生改变的时候，是否需要重新刷新布局
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds{
    return YES;
}

// cell item Left Aligned(左对齐)
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect{
    NSArray *array = [[NSArray alloc] initWithArray:[super layoutAttributesForElementsInRect:rect] copyItems:YES];
    
    CGFloat Y = 0;
    CGFloat X = self.sectionInset.left;
    for (UICollectionViewLayoutAttributes *attrs in array) {
        if (attrs.representedElementCategory == UICollectionElementCategoryCell){
            if (attrs.indexPath.row == 0) {
                //  item是一行一行的显示，第一个元素肯定是一行的第一个元素
                Y = attrs.frame.origin.y;
            }else {
                if (Y != attrs.frame.origin.y) { // 换行
                    X = self.sectionInset.left;  // 新的行第一个的X值
                    Y = attrs.frame.origin.y;    // 保存新行的Y值
                }
            }
            CGRect frame = attrs.frame;
            frame.origin = CGPointMake(X, Y);
            attrs.frame = frame;
            X = CGRectGetMaxX(frame) + self.minimumInteritemSpacing;
        }
    }
    return array;
}

@end
